package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IRedisService;
import cn.wolfcode.wolf2w.redis.test.RedisByKey;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static cn.wolfcode.wolf2w.util.JsonResult.success;

@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/get")
    public Object get(String id){
        return userInfoService.selectOne(id);
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        boolean checkResult=userInfoService.checkPhone(phone);
        return !checkResult;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        //1、获取手机号码
        //2、生成4位的验证码
        String code1=userInfoService.getCode(phone);
        //3、模拟用信息发送验证码到用户手机中;
        //4、将手机号码和验证码储存在redis中
        redisService.saveRedis(phone, code1);
        return JsonResult.success();
    }

    @PostMapping("/regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode){
        userInfoService.regist(phone,nickname,password,rpassword,verifyCode);
        return JsonResult.success();
    }

    @PostMapping("/login")
    public Object login(String username,String password){
        //判断该用户是否存在
        UserInfo userInfo=userInfoService.login(username,password);
        Map<String,Object> map =new HashMap<>();
        if(userInfo!=null){
            //若不为空，将在redis服务层中生成令牌
            String token = redisService.setToken(userInfo);
            //将用户对象和token转成json的格式传到前端页面
            String user = JSON.toJSONString(userInfo);
            map.put("token",token);
            map.put("userInfo", user);
        }
        return JsonResult.success(map);
    }

    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){
        String token = request.getHeader("token");
        System.out.println(token);
        UserInfo userInfo=new UserInfo();
        String user=null;
        //判断token是否为空
        if(StringUtils.hasLength(token)){
            //对key进行拼接
            String key = RedisByKey.USER_TOKEN.join(token);
            //再判断key是否失效
            if(redisTemplate.hasKey(key)){
                user = (String) redisTemplate.opsForValue().get(key);
                //将json格式转成UserInfo格式
                userInfo = JSON.parseObject(user, UserInfo.class);
                System.out.println(userInfo);
                //重置有效时间
                redisTemplate.expire(key, 30, TimeUnit.MINUTES);
            }
        }
        return JsonResult.success(user);
    }

}
